package com.atguigu.dailyservice.service.impl;

import com.atguigu.dailyservice.client.UMemberClient;
import com.atguigu.dailyservice.entity.StatisticsDaily;
import com.atguigu.dailyservice.mapper.StatisticsDailyMapper;
import com.atguigu.dailyservice.service.StatisticsDailyService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author test.java
 * @since 2023-03-25
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Resource
	private StatisticsDailyMapper statisticsDailyMapper;
	@Resource
	private UMemberClient uMemberClient;

	@Override
	public void createStatisticsByDay(String day) {
		//删除已存在的统计对象
		QueryWrapper<StatisticsDaily> dayQueryWrapper = new QueryWrapper<>();
		dayQueryWrapper.eq("date_calculated", day);
		statisticsDailyMapper.delete(dayQueryWrapper);

		//获取统计信息
		Integer registerNum = (Integer) uMemberClient.registerCount(day).getData().get("countRegister");
		Integer loginNum = RandomUtils.nextInt(100, 200);//TODO
		Integer videoViewNum = RandomUtils.nextInt(100, 200);//TODO
		Integer courseNum = RandomUtils.nextInt(100, 200);//TODO

		//创建统计对象
		StatisticsDaily daily = new StatisticsDaily();
		daily.setRegisterNum(registerNum);
		daily.setLoginNum(loginNum);
		daily.setVideoViewNum(videoViewNum);
		daily.setCourseNum(courseNum);
		daily.setDateCalculated(day);
		statisticsDailyMapper.insert(daily);
	}

	@Override
	public Map<String, Object> getChartData(String begin, String end, String type) {
		QueryWrapper<StatisticsDaily> dayQueryWrapper = new QueryWrapper<>();
		//查询的范围->根据日期确定
		dayQueryWrapper.between("date_calculated", begin, end);
         //确定要查询的字段->用QueryWrapper可以设置动态查询
		dayQueryWrapper.select(type, "date_calculated");
         //List集合和map集合可以转化成json数组->以 @ResponseBody的形式返回数据->map或者list会转化成数组
		List<StatisticsDaily> dayList = statisticsDailyMapper.selectList(dayQueryWrapper);

		Map<String, Object> map = new HashMap<>();
		List<String> dataCalculateList = new ArrayList<>();
		List<Integer> NumDateList = new ArrayList<>();

		for (StatisticsDaily daily : dayList) {
			dataCalculateList.add(daily.getDateCalculated());
			switch (type) {
				case "register_num":
					NumDateList.add(daily.getRegisterNum());
					break;
				case "login_num":
					NumDateList.add(daily.getLoginNum());
					break;
				case "video_view_num":
					NumDateList.add(daily.getVideoViewNum());
					break;
				case "course_num":
					NumDateList.add(daily.getCourseNum());
					break;
				default:
					break;
			}
		}

		map.put("NumDateList", NumDateList);
		map.put("dataCalculateList",dataCalculateList);

		return map;


	}


}
